VERSION 5.00
Begin VB.Form Dialog 
   BorderStyle     =   3  'Fixed Dialog
   Caption         =   "Add a new version"
   ClientHeight    =   5130
   ClientLeft      =   2760
   ClientTop       =   3750
   ClientWidth     =   6420
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MDIChild        =   -1  'True
   MinButton       =   0   'False
   ScaleHeight     =   5130
   ScaleWidth      =   6420
   ShowInTaskbar   =   0   'False
   Begin VB.TextBox txtVer 
      Appearance      =   0  'Flat
      Height          =   405
      Left            =   1920
      TabIndex        =   7
      Top             =   720
      Width           =   4215
   End
   Begin VB.TextBox txtDes 
      Appearance      =   0  'Flat
      Height          =   1215
      Left            =   1920
      MultiLine       =   -1  'True
      ScrollBars      =   2  'Vertical
      TabIndex        =   6
      Top             =   1440
      Width           =   4215
   End
   Begin VB.ComboBox cmbCheck 
      Height          =   315
      Left            =   1920
      TabIndex        =   5
      Top             =   3000
      Width           =   4215
   End
   Begin VB.CommandButton CancelButton 
      Caption         =   "Cancel"
      Height          =   375
      Left            =   3000
      TabIndex        =   1
      Top             =   3840
      Width           =   1215
   End
   Begin VB.CommandButton OKButton 
      Caption         =   "OK"
      Height          =   375
      Left            =   4680
      TabIndex        =   0
      Top             =   3840
      Width           =   1215
   End
   Begin VB.Label Label2 
      AutoSize        =   -1  'True
      Caption         =   "Complusory:"
      DataField       =   "Version Number"
      Height          =   195
      Left            =   480
      TabIndex        =   4
      Top             =   3000
      Width           =   855
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "Description:"
      DataField       =   "Version Number"
      Height          =   195
      Left            =   480
      TabIndex        =   3
      Top             =   1320
      Width           =   960
   End
   Begin VB.Label VN 
      AutoSize        =   -1  'True
      Caption         =   "Version Number:"
      DataField       =   "Version Number"
      Height          =   195
      Left            =   480
      TabIndex        =   2
      Top             =   720
      Width           =   1170
   End
End
Attribute VB_Name = "Dialog"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Option Explicit
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset


Private Sub CancelButton_Click()
    Unload Me
End Sub

Private Sub CloseRecordSet(ByRef rs As ADODB.Recordset)
On Error GoTo NoErr
        rs.Close
NoErr:
End Sub

Private Sub Form_Load()
    Call Cn2SQL(cn)
     
    cmbCheck.AddItem "NO"
    cmbCheck.AddItem "YES"
    
    cmbCheck.ListIndex = 0
End Sub

Private Sub Form_Unload(Cancel As Integer)
    cn.Close
End Sub

Private Sub OKButton_Click()
    Dim sql As String
    If CheckVersionNumber = True Then
        sql = "INSERT INTO `fota`(`VerNumber`, `Des`, `status`) VALUES (" & val(Trim(txtVer.Text)) & ",'" & Trim(txtDes.Text) & "', " & cmbCheck.ListIndex & ")"
        'Debug.Print sql
        Call CloseRecordSet(rs)
        OperateDatabase rs, cn, sql
        MsgBox "New version added!", vbOKOnly, SOFT_TITLE
        Unload Me
    End If
End Sub

Private Function CheckVersionNumber() As Boolean
    Dim sql As String
    CheckVersionNumber = False
    'On Error GoTo openDB
    'sql = "SET NAMES gbk"
    'Call CloseRecordSet(rs)
    'OperateDatabase rs, cn, sql
openDB:
    sql = "select max(VerNumber) from `fota`"
    Call CloseRecordSet(rs)
    OperateDatabase rs, cn, sql
    If Not rs.EOF Then
        If val(rs.Fields(0).value) >= val(Trim(txtVer.Text)) Then
            MsgBox "The version number you input is not correct! Please try it again", vbOKOnly, SOFT_TITLE
            txtVer.SetFocus
        Else
            CheckVersionNumber = True
        End If
    End If
End Function
